/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.weeho.dongyong.utils;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 *
 * @author Administrator
 */
public class PhoneUtils {

    /**
     *
     * @param telephone
     * @param code
     * @throws Exception
     */
    public static void sendToRomotApi(String telephone, String code) throws Exception {
        InputStream stream = PhoneUtils.class.getResourceAsStream("/template/send_message_tmplate.xml");//this.getClass().getResourceAsStream();
        String template = StringHelper.streamToString(stream, "utf-8");
        //log.info("template is "+template);
        String xml = template.replace("${mobileNum}", telephone);
        xml = xml.replace("${checkCode}", code);
		//Request.Post("http://sms.9hgame.com/JHSMSService.asmx");

        //Request.Post("http://sms.9hgame.com/JHSMSService.asmx").bodyString(xml, null).execute().returnContent();
        //log.info(xml);
        HttpClient httpClient = new HttpClient();
        PostMethod post = new PostMethod("http://sms.9hgame.com/JHSMSService.asmx");
        post.addRequestHeader("Content-Type", "text/xml; charset=utf-8");//在头文件中设置转码
        byte[] bytes = xml.getBytes("utf-8");
        InputStream inputStream = new ByteArrayInputStream(bytes, 0, bytes.length);
        RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, bytes.length, "text/xml; charset=utf-8");
        post.setRequestEntity(requestEntity);
        httpClient.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        //log.info("statusCode:"+statusCode);
        for (Header h : headers) {
            //log.info(h);
        }
        String result = new String(post.getResponseBodyAsString().getBytes("utf8"));
        //log.info("send message result: "+result);
        //System.out.println(result);
    }
}
